<?php
class TplUserQuestion {
	public static function TopMenu($tit=false) {
	}

	private static function NList($data) {
		$GLOBALS['jscripts'][] = '/js/module_question.js';
		$lang=Eleanor::$Language[$GLOBALS['Eleanor']->module['config']['n']];
		$c = '';
		foreach($data['items'] as $v) {
			if (isset($v['navi'])) {
				$ca = $lang['category'];
				foreach($v['navi'] as $k => $cat_name) {
					$ca .= ' <a href="'. $GLOBALS['Eleanor']->Url->Construct(array('module' => $GLOBALS['Eleanor']->module['name']) + $GLOBALS['Eleanor']->Categories->GetUri($k), false, $GLOBALS['Eleanor']->Url->delimiter, 1) .'" title="'.strip_tags($GLOBALS['Eleanor']->Categories->dump[$k]['description']).'">'. $cat_name .'</a>';
				}
			} elseif(isset($data['ucats'][$v['_cat']])) {
				$ca = sprintf($lang['category_'], '<a href="'. $data['ucats'][$v['_cat']] .'" title="'.strip_tags($GLOBALS['Eleanor']->Categories->dump[$v['_cat']]['description']).'">'.$GLOBALS['Eleanor']->Categories->dump[$v['_cat']]['title'].'</a>');
			}

			$c .= Eleanor::$Template->BaseQuestion(array(
				'top' => array(
					'published' => sprintf($lang['published_'], Eleanor::$Language->Date($v['date'], 'fdt')),
					'category' => isset($ca) ? $ca : false,
					'author_name' => $v['author'] ? $v['author'] : 'Anonim',
				),
				'bottom' => array(
					'readmore' => '<a href="'.$v['_url'].'#more">'. $lang['readmore'] .'</a>',
				),
				'title' => '<a href="'.$v['_url'].'">'. $v['title'] .'</a>',
				'text' => Strings::CutStr($v['text'], 180),
				'comments_count' => $lang['comments_count'] .'<span>'. $v['comments_count'] .'</span>',
			));
		}
		return $c  .'<script>//<![CDATA[
$(function(){CORE.Question.Init("article.jaunumi_atbilde_main_question", "'.$GLOBALS['Eleanor']->module['name'].'");})//]]></script>';
	}

	public static function QuestionList($data,$cnt,$page, $pages, $pp, $links) {
        return static::NList($data) . Eleanor::$Template->Pages($cnt, $pp, $page, array($links['pages'],$pages=>$links['first_page'])) .'</section>';
	}

//	public static function QuestionDateList($date,$data,$cnt,$page,$pp) {
//		return static::TopMenu(reset($GLOBALS['title'])) . static::NList($data) . Eleanor::$Template->Pages($cnt,$pp,-$page,$GLOBALS['Eleanor']->Url->Construct(array('do'=>$date,array('page'=>'{page}')), true, ''));
//	}

//	public static function QuestionAuthorList($data,$cnt,$page,$pp) {
//		return static::TopMenu(reset($GLOBALS['title'])) . static::NList($data) . Eleanor::$Template->Pages($cnt, $pp, $page,$GLOBALS['Eleanor']->Url->Construct(array('do' => 'my', array('page' => '{page}')), true, ''));
//	}

	public static function QuestionCategoryList($category, $data, $cnt, $page, $pp) {
		$cu = $GLOBALS['Eleanor']->Categories->GetUri($category['id']);
		return static::ShowCategories($category['id']) .'<div style="clear: both">'. static::NList($data) .'</div>' . Eleanor::$Template->Pages($cnt,$pp,-$page,$GLOBALS['Eleanor']->Url->Construct($cu + array('page' => array('page'=>'{page}')), true, $GLOBALS['Eleanor']->Url->delimiter));
	}

	private static function ShowCategories($cat = 0, $data = false) {
		$c = '<section id="categories_tab">';
		if (isset($GLOBALS['Eleanor']->Categories->dump[$cat])) {
            foreach($GLOBALS['Eleanor']->Categories->dump as $v) {
                if (in_array($GLOBALS['Eleanor']->Categories->dump[$cat]['id'], explode(',', $v['parents']))) {
                    $c .= static::CategoryItem($v);
                }
            }
		} else {
            foreach($GLOBALS['Eleanor']->Categories->dump as $v) {
                $c .= static::CategoryItem($v);
            }
			return $c .'</section>'. ($data ? static::NList($data) : null);
		}
		return $c .'</section>';
	}

	private static function CategoryItem(&$v) {
        //Shit, because in templates dont need doing query to db
        $v['count'] = current(Eleanor::$Db->Query('SELECT COUNT(*) AS `count_item` FROM `el_question_l` AS `tm` WHERE  `tm`.`lcats` LIKE \'%,'. $v['id'] .',%\' AND `lstatus` = 1 LIMIT 1')->fetch_row());

		$image = $v['image'] ? $v['image'] : '/templates/AutoDiagnostika/images/sludinajumi_kategorijas/instruments.png';
		return '<div class="sludinajuma_delis_kategorijas">
			<a href="'. $GLOBALS['Eleanor']->Url->Construct($GLOBALS['Eleanor']->Categories->GetUri($v['id']), true, $GLOBALS['Eleanor']->Url->delimiter) .'" title="'. htmlspecialchars($v['title'], ENT_QUOTES) .'">
				<div class="sludinajuma_delis_kategorijas_image_container">
					<div class="sludinajuma_delis_kategorijas_image_centering">
						<img src="'.  $image .'" alt="'. htmlspecialchars($v['title'], ENT_QUOTES) .'">
					</div>
				</div>
				<div class="sludinajuma_delis_kategorijas_text">'. $v['title'] .'<span>('. (int)$v['count'] .')</span></div>
			</a>
		</div>';
	}

	public static function ShowQuestion($arr, $comments) {
        $category = '';
		$lang = Eleanor::$Language[$GLOBALS['Eleanor']->module['config']['n']];
		$GLOBALS['Eleanor']->Url->hash = 'more';

        if (isset($arr['navi'])) {
            $category = $lang['category'] .':';
            foreach($arr['navi'] as $k => $cat_name) {
                $category .= ' <a href="'. $GLOBALS['Eleanor']->Url->Construct(array('module' => $GLOBALS['Eleanor']->module['name']) + $GLOBALS['Eleanor']->Categories->GetUri($k), false, $GLOBALS['Eleanor']->Url->delimiter, 1) .'" title="'.strip_tags($GLOBALS['Eleanor']->Categories->dump[$k]['description']).'">'. $cat_name .'</a>';
            }
        }

		return static::TopMenu()
			. Eleanor::$Template->BaseQuestionOne(array(
				'top'=>array(
					'published'=>sprintf($lang['published_'],Eleanor::$Language->Date($arr['date'],'fdt')),
					'category'=>$category ? $category : false,
					'author'=>sprintf($lang['publisher_'],$arr['author_id'] ? '<a href="'.Eleanor::$Login->UserLink($arr['author'],$arr['author_id']).'">'.$arr['author'].'</a>' : $arr['author']),
					'reads'=>sprintf($lang['reads_'],$arr['reads']),
				),
				'title' => $arr['title'],
				'text' => $arr['text'],
			)) . $comments;
	}

	public static function DisplayQuestion($data = null, $lang) {
		return Eleanor::$Template->BaseQuestionForm(array(
				'data' => $data,
				'lang' => $lang,
			));
	}
}